package vincent.com.layouttest;

import android.content.Context;
import android.graphics.Color;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import vincent.com.layouttest.util.Utils;

/**
 * Created by chenzhengwen on 2015/6/25.
 */
public class LANSnifferActivity extends ActionBarActivity {
    private Button snifferBtn;
    private TextView snifferResult;
    private EditText snifferIpAddr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lan_sniffer);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);

        findViewById(R.id.most_outer_container).setPadding(0, getStatusBarHeight(), 0, 0);

        snifferResult = (TextView)findViewById(R.id.sniffer_result);
        snifferIpAddr = (EditText)findViewById(R.id.sniffer_ip_addr);

        snifferBtn = (Button)findViewById(R.id.sniffer_btn);
        snifferBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(snifferIpAddr.getText().toString()) || snifferIpAddr.getText().toString().trim().length() == 0){
                    Toast.makeText(LANSnifferActivity.this, "ip is null", Toast.LENGTH_SHORT).show();
                    return;
                }

               /* WifiManager mWifiManager = (WifiManager)LANSnifferActivity.this.getSystemService(Context.WIFI_SERVICE);
                DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
                int dns1 = dhcpInfo.dns1;
                int dns2 = dhcpInfo.dns2;

                String dns1Str = String.format("%d.%d.%d.%d", (dns1 & 0xff), (dns1 >> 8 & 0xff), (dns1 >> 16 & 0xff), (dns1 >> 24 & 0xff));
                String dns2Str = String.format("%d.%d.%d.%d", (dns2 & 0xff), (dns2 >> 8 & 0xff), (dns2 >> 16 & 0xff), (dns2 >> 24 & 0xff));
*/
                String result = Utils.execCommand("ping -c 2 " + snifferIpAddr.getText().toString().trim());
                snifferResult.setText(result);
            }
        });
    }

    public int getStatusBarHeight(){
        int height = 0;
        int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        height = getResources().getDimensionPixelSize(resId);

        return height;
    }
}
